//
//  WBPopOverView.m
//  pop_test
//
//  Created by tuhui－03 on 16/5/19.
//  Copyright © 2016年 tuhui－03. All rights reserved.
//

#import "WBPopOverView.h"
#import "HomeModalCell.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define ARROW_SPACE 5.0
//标题之间的分割线颜色
#define SERVER_MARKET_SPACE_COLOR @"c9c9c9"
//拐角半径
#define CORNER_RADIUS_APP 2.5
//文字颜色
#define TEXT_COLOR @"ffffff"
@interface WBPopOverView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) WBArrowDirection direction;

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *lineView;

@end

@implementation WBPopOverView

//懒加载
- (NSArray *)dataArray {
    if (!_dataArray) {
        NSDictionary *dic1 = @{@"image":@"",
                               @"title":NSLocalString(@"办理中")};
        NSDictionary *dic2 = @{@"image":@"",
                               @"title":NSLocalString(@"已结办")};
        _dataArray = @[dic1, dic2];
    }
    return _dataArray;
}

-(instancetype)initWithOrigin:(CGPoint)origin Width:(CGFloat)width Height:(float)height Direction:(WBArrowDirection)direction
{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]){
        //背景颜色为无色
        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.1];
        //定义显示视图的参数
        self.origin = origin;
        self.height = height;
        self.width = width;
        self.direction = direction;
        
        self.backView = [[UIView alloc]initWithFrame:CGRectMake(origin.x, origin.y, width, height)];
        self.backView.backgroundColor=[UIColor colorWithHexString:SERVER_MARKET_SPACE_COLOR];
        self.backView.layer.cornerRadius = CORNER_RADIUS_APP;
        self.backView.layer.masksToBounds = YES;
        self.backView.alpha = 0.0f;
        [self addSubview:self.backView];
      
        [self initTableView];
        [self initLineView];
    }

    return self;
}

- (void)initLineView {
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:self.lineView];
    [self.backView bringSubviewToFront:self.lineView];
}

//初始化 tableView
- (void)initTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.5, 0.5, self.backView.bounds.size.width - 1, self.backView.bounds.size.height - 1) style: UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.layer.cornerRadius = CORNER_RADIUS_APP;
    self.tableView.layer.masksToBounds = YES;
    
    self.tableView.bounces = NO;
    
    //注册 cell
//    [self.tableView registerNib:[UINib nibWithNibName:@"HomeModalCell" bundle:nil] forCellReuseIdentifier:@"HomeModalCell"];
    [self.tableView registerClass:[HomeModalCell class] forCellReuseIdentifier:@"HomeModalCell"];
    
    [self.backView addSubview:self.tableView];
}

-(void)drawRect:(CGRect)rect{
    CGContextRef context=UIGraphicsGetCurrentContext();
    
    if (_direction==WBArrowDirectionLeft1) {
        
        
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX+ARROW_SPACE, startY-ARROW_SPACE);
        CGContextAddLineToPoint(context, startX+ARROW_SPACE, startY+ARROW_SPACE);
    }
    else if (_direction==WBArrowDirectionLeft2)
    {
    
        
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX+ARROW_SPACE, startY-ARROW_SPACE);
        CGContextAddLineToPoint(context, startX+ARROW_SPACE, startY+ARROW_SPACE);
    
    }
    else if (_direction==WBArrowDirectionLeft3)
    {

        
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX+ARROW_SPACE, startY-ARROW_SPACE);
        CGContextAddLineToPoint(context, startX+ARROW_SPACE, startY+ARROW_SPACE);
        
    }
    else if (_direction==WBArrowDirectionRight1)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX-ARROW_SPACE, startY-ARROW_SPACE);
        CGContextAddLineToPoint(context, startX-ARROW_SPACE, startY+ARROW_SPACE);
        
    }
    else if (_direction==WBArrowDirectionRight2)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX-ARROW_SPACE, startY-ARROW_SPACE);
        CGContextAddLineToPoint(context, startX-ARROW_SPACE, startY+ARROW_SPACE);
        
    }
    else if (_direction==WBArrowDirectionRight3)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX-ARROW_SPACE, startY-ARROW_SPACE);
        CGContextAddLineToPoint(context, startX-ARROW_SPACE, startY+ARROW_SPACE);
        
    }
    else if (_direction==WBArrowDirectionUp1)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX + ARROW_SPACE, startY +ARROW_SPACE);
        CGContextAddLineToPoint(context, startX -ARROW_SPACE, startY+ARROW_SPACE);
        
    }
    else if (_direction==WBArrowDirectionUp2)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX + ARROW_SPACE, startY +ARROW_SPACE);
        CGContextAddLineToPoint(context, startX -ARROW_SPACE, startY+ARROW_SPACE);
    }
    else if (_direction==WBArrowDirectionUp3)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX + ARROW_SPACE, startY + ARROW_SPACE + 1);
        CGContextAddLineToPoint(context, startX - ARROW_SPACE, startY + ARROW_SPACE + 1);
        
    }
    else if (_direction==WBArrowDirectionDown1)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX - ARROW_SPACE, startY - ARROW_SPACE);
        CGContextAddLineToPoint(context, startX +ARROW_SPACE, startY-ARROW_SPACE);
        
    }
    else if (_direction==WBArrowDirectionDown2)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX - ARROW_SPACE, startY -ARROW_SPACE);
        CGContextAddLineToPoint(context, startX +ARROW_SPACE, startY-ARROW_SPACE);
        
    }
    else if (_direction==WBArrowDirectionDown3)
    {
        CGFloat startX = self.origin.x;
        CGFloat startY = self.origin.y;
        CGContextMoveToPoint(context, startX, startY);//设置起点
        CGContextAddLineToPoint(context, startX - ARROW_SPACE, startY -ARROW_SPACE);
        CGContextAddLineToPoint(context, startX +ARROW_SPACE, startY-ARROW_SPACE);
        
    }
   
    CGContextSetLineWidth(context, 0.5);
    CGContextClosePath(context);
    [[UIColor colorWithHexString:TEXT_COLOR] setFill];
    [self.backView.backgroundColor setStroke];
    CGContextDrawPath(context, kCGPathFillStroke);
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (![touch.view isEqual:self.backView]) {
        [self dismiss];
    }
}

-(void)popView
{
    NSArray *result=[self.backView subviews];
    for (UIView *view in result) {
        
        view.hidden=YES;
        
    }
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    //动画效果弹出
    self.alpha = 0;
    
    
    if (_direction==WBArrowDirectionLeft1) {
        self.backView.frame = CGRectMake(self.origin.x+ARROW_SPACE, self.origin.y, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x+ARROW_SPACE, self.origin.y-20, self.width,self. height);
        }completion:^(BOOL finished) {
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
        }];
        
        
    }
    else if (_direction==WBArrowDirectionLeft2)
    {
        self.backView.frame = CGRectMake(self.origin.x+ARROW_SPACE, self.origin.y, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x+ARROW_SPACE, self.origin.y-self.height/2, self.width,self. height);

        }completion:^(BOOL finished) {
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
        }];
    }
    else if (_direction==WBArrowDirectionLeft3)
    {
        self.backView.frame = CGRectMake(self.origin.x+ARROW_SPACE, self.origin.y, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x+ARROW_SPACE, self.origin.y-self.height+20, self.width,self. height);
        }completion:^(BOOL finished) {
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
        }];
        
    }
    else if (_direction==WBArrowDirectionRight1)
    {
        self.backView.frame = CGRectMake(self.origin.x-ARROW_SPACE, self.origin.y, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x-ARROW_SPACE, self.origin.y-20, -self.width,self. height);
        }completion:^(BOOL finished) {
            
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }
    else if (_direction==WBArrowDirectionRight2)
    {
        self.backView.frame = CGRectMake(self.origin.x-ARROW_SPACE, self.origin.y, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x-ARROW_SPACE, self.origin.y-self.height/2, -self.width,self. height);
        }completion:^(BOOL finished) {
            
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }
    else if (_direction==WBArrowDirectionRight3)
    {
        self.backView.frame = CGRectMake(self.origin.x-ARROW_SPACE, self.origin.y, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x-ARROW_SPACE, self.origin.y-self.height+20, -self.width,self. height);
        }completion:^(BOOL finished) {
            
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }
    else if (_direction==WBArrowDirectionUp1)
    {
        self.backView.frame = CGRectMake(self.origin.x, self.origin.y+ARROW_SPACE, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x-20, self.origin.y+ARROW_SPACE, self.width,self. height);
        }completion:^(BOOL finished) {
            
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }
    else if (_direction==WBArrowDirectionUp2)
    {
        self.backView.frame = CGRectMake(self.origin.x, self.origin.y+ARROW_SPACE, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x-self.width/2, self.origin.y+ARROW_SPACE, self.width,self. height);
        }completion:^(BOOL finished) {
            
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }
    else if (_direction==WBArrowDirectionUp3)
    {
        self.backView.frame = CGRectMake(self.origin.x, self.origin.y+ARROW_SPACE, 0, 0);
        self.lineView.frame = CGRectMake(0, 0, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 0.5;
            self.backView.frame = CGRectMake(self.origin.x + 10, self.origin.y+ARROW_SPACE, -self.width, self.height);
            self.lineView.frame = CGRectMake(self.width - ARROW_SPACE * 3 + 1, 0, 2 *ARROW_SPACE - 2, 0.5);
        }completion:^(BOOL finished) {
            
            self.backView.alpha = 1.0;
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }
    else if (_direction==WBArrowDirectionDown1)
    {
        self.backView.frame = CGRectMake(self.origin.x, self.origin.y-ARROW_SPACE, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x-20, self.origin.y-ARROW_SPACE, self.width,-self. height);
        }completion:^(BOOL finished) {
            
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }
    else if (_direction==WBArrowDirectionDown2)
    {
        self.backView.frame = CGRectMake(self.origin.x, self.origin.y-ARROW_SPACE, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x-self.width/2, self.origin.y-ARROW_SPACE, self.width,-self. height);
        }completion:^(BOOL finished) {
            
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }
    else if (_direction==WBArrowDirectionDown3)
    {
        self.backView.frame = CGRectMake(self.origin.x, self.origin.y-ARROW_SPACE, 0, 0);
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha = 1;
            self.backView.alpha = 1;
            self.backView.frame = CGRectMake(self.origin.x-self.width+20, self.origin.y-ARROW_SPACE, self.width,-self. height);
        }completion:^(BOOL finished) {
            
            NSArray *result=[self.backView subviews];
            for (UIView *view in result) {
                
                view.hidden=NO;
                
            }
            
        }];
        
    }

}

-(void)dismiss{
    
    NSArray *result=[self.backView subviews];
    for (UIView *view in result) {
        
        [view removeFromSuperview];

    }
    
         //动画效果淡出
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
        self.backView.alpha = 0;
        self.backView.frame = CGRectMake(self.origin.x, self.origin.y, 0, 0);
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
                 
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeModalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeModalCell" forIndexPath:indexPath];
    
    NSDictionary *dic = self.dataArray[indexPath.row];
    
    if ([dic[@"image"] isEqualToString:@""]) {
        [cell.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(cell.mas_centerY);
            make.left.mas_equalTo(cell.mas_left).offset(20 * m6Scale);
            make.right.mas_equalTo(cell.mas_right).offset(-20 * m6Scale);
        }];
        cell.leftImageView.image = nil;
        cell.titleLabel.text =  dic[@"title"];
    }
    else {
        cell.leftImageView.image = [UIImage imageNamed:dic[@"image"]];
        cell.titleLabel.text =  dic[@"title"];
    }
    
    if (indexPath.row == self.dataArray.count - 1) {
        [cell setLineHidden:YES];
    }else {
        [cell setLineHidden:NO];
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.backView.bounds.size.height / self.dataArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self dismiss];
    
    if ([self.delegate respondsToSelector:@selector(clickItemWithIndexPath:)]) {
        [self.delegate clickItemWithIndexPath:indexPath];
    }
}

@end

